/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.apache.myfaces.test.webapp.impl.metadata.assertion;

import net.sf.cglib.proxy.MethodProxy;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Cosmin Martinconi, 19.07.2010
 */
public class AssertableProxyMethod implements AssertableValue {

    private static final Logger log = Logger.getLogger(AssertableProxyMethod.class.getName());

    private Object methodObj;
    private Object[] methodArgs;
    private MethodProxy methodProxy;

    public AssertableProxyMethod() {
        this(null, null, null);
    }

    public AssertableProxyMethod(Object methodObj, Object[] methodArgs,
                                 MethodProxy methodProxy) {
        this.methodObj = methodObj;
        this.methodArgs = methodArgs;
        this.methodProxy = methodProxy;
    }

    public Object evaluate() {

        if (methodProxy != null) {
            try {
                return methodProxy.invoke(methodObj, methodArgs);

            } catch (Throwable throwable) {
                log.log(Level.SEVERE, "Could not invoke method " + methodProxy, throwable);
            }
        }

        return null;
    }

    public Object getMethodObj() {
        return methodObj;
    }

    public Object[] getMethodArgs() {
        return methodArgs;
    }

    public MethodProxy getMethodProxy() {
        return methodProxy;
    }

    public void setMethodObj(Object methodObj) {
        this.methodObj = methodObj;
    }

    public void setMethodArgs(Object[] methodArgs) {
        this.methodArgs = methodArgs;
    }

    public void setMethodProxy(MethodProxy methodProxy) {
        this.methodProxy = methodProxy;
    }
}
